<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .el-table .warning-row {
            background: oldlace;
        }
        
        .el-table .success-row {
            background: #f0f9eb;
        }
    </style>

    <script src="js/axios-0.18.0.js"></script>
    <script src="js/vue.js"></script>
    <script src="element-ui/lib/index.js"></script>
    <link rel="stylesheet" href="element-ui/lib/theme-chalk/index.css">

</head>

<body>
    <div id="app">

        <!--搜索表单-->
        <el-form :inline="true" :model="brand" class="demo-form-inline">

            <el-form-item label="当前状态">
                <el-select v-model="brand.status" placeholder="当前状态">
                    <el-option label="启用" value="1"></el-option>
                    <el-option label="禁用" value="0"></el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="企业名称">
                <el-input v-model="brand.companyName" placeholder="企业名称"></el-input>
            </el-form-item>

            <el-form-item label="品牌名称">
                <el-input v-model="brand.brandName" placeholder="品牌名称"></el-input>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="onSubmit">查询</el-button>
            </el-form-item>
        </el-form>

        <!--按钮-->

        <el-row>

            <el-button type="danger" plain @click="delPatch">批量删除</el-button>
            <el-button type="primary" plain @click="dialogVisible = true">新增</el-button>

        </el-row>
        <!--添加数据对话框表单-->
        <el-dialog title="编辑品牌" :visible.sync="dialogVisible" width="30%">

            <el-form ref="form" :model="brand" :rules="rules" label-width="80px">
                <el-form-item label="品牌名称" prop="brandName">
                    <el-input v-model="brand.brandName"></el-input>
                </el-form-item>

                <el-form-item label="企业名称" prop="companyName">
                    <el-input v-model="brand.companyName"></el-input>
                </el-form-item>

                <el-form-item label="排序">
                    <el-input v-model="brand.ordered"></el-input>
                </el-form-item>

                <el-form-item label="备注">
                    <el-input type="textarea" v-model="brand.description"></el-input>
                </el-form-item>

                <el-form-item label="状态">
                    <el-switch v-model="brand.status" active-value="1" inactive-value="0"></el-switch>
                </el-form-item>


                <el-form-item>
                    <el-button type="primary" @click="addBrand">提交</el-button>
                    <el-button @click="dialogVisible = false">取消</el-button>
                </el-form-item>
            </el-form>

        </el-dialog>

        <!--修改数据对话框表单-->
        <el-dialog title="修改品牌" :visible.sync="dialogVisible1" width="30%">

            <el-form ref="form" :model="newbrand" :rules="rules" label-width="80px">
                <el-form-item label="品牌名称" prop="brandName">
                    <el-input v-model="newBrand.brandName"></el-input>
                </el-form-item>

                <el-form-item label="企业名称" prop="companyName">
                    <el-input v-model="newBrand.companyName"></el-input>
                </el-form-item>

                <el-form-item label="排序">
                    <el-input v-model="newBrand.ordered"></el-input>
                </el-form-item>

                <el-form-item label="备注">
                    <el-input type="textarea" v-model="newBrand.description"></el-input>
                </el-form-item>

                <el-form-item label="状态">
                    <el-switch v-model="newBrand.status" active-value="1" inactive-value="0"></el-switch>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="updateBrand">提交</el-button>
                    <el-button @click="dialogVisible1 = false">取消</el-button>
                </el-form-item>
            </el-form>

        </el-dialog>

        <!--表格-->
        <template>
        <el-table
                :data="tableData"
                style="width: 100%"
                :row-class-name="tableRowClassName"
                @selection-change="handleSelectionChange"
        >
            <el-table-column
                    type="selection"
                    width="55">
            </el-table-column>

            <el-table-column
                    type="index"
                    width="50">
            </el-table-column>

            <el-table-column
                    prop="brandName"
                    label="品牌名称"
                    align="center"
            >
            </el-table-column>
            <el-table-column
                    prop="companyName"
                    label="企业名称"
                    align="center"
            >
            </el-table-column>
            <el-table-column
                    prop="ordered"
                    align="center"
                    label="排序">
            </el-table-column>
            <el-table-column
                    prop="status"
                    align="center"
                    label="当前状态">
            </el-table-column>

            <el-table-column
                    align="center"
                    label="操作">


                <template slot-scope="scope">
                    <el-button type="primary" @click="changeBrand(scope)">修改</el-button>
                    <el-button type="danger" @click="deleteById(scope.row.id)">删除</el-button>
                  </template>


        </el-table-column>

        </el-table>
        </template>

        <!--分页工具条-->
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[5, 10, 15, 20]" :page-size="Pagesize" layout="total, sizes, prev, pager, next, jumper" :total="totalCount">
        </el-pagination>

    </div>

    <script>
        new Vue({
            el: "#app",
            methods: {
                tableRowClassName({
                    row,
                    rowIndex
                }) {
                    if (rowIndex === 1) {
                        return 'warning-row';
                    } else if (rowIndex === 3) {
                        return 'success-row';
                    }
                    return '';
                },
                // 复选框选中后执行的方法
                handleSelectionChange(val) {
                    this.multipleSelection = val;
                    //console.log(val);
                    //console.log(this.multipleSelection)
                },
                // 查询方法
                onSubmit() {
                    axios.post("http://localhost/selectByPageAndCondition?currentPage=" + this.currentPage + "&pageSize=" + this.Pagesize, this.brand).then((resp) => {
                        //刷新数据
                        this.tableData = resp.data.rows;
                        //总记录数
                        this.totalCount = resp.data.totalCount;
                    })

                },
                // 添加数据
                addBrand() {
                    this.dialogVisible = false;
                    axios.post("http://localhost/brandadd", this.brand).then((resp) => {
                        //console.log(resp.data);
                        if (resp.data == "sucess") {
                            this.getBrandList();
                            this.$message({
                                message: '恭喜你,添加成功',
                                type: 'success'
                            });
                        } else {
                            this.$message({
                                message: '很遗憾,添加失败',
                                type: 'error'
                            });
                        }
                    })
                },

                //分页
                handleSizeChange(val) {
                    // 重新设置每页显示的条数
                      
                    this.Pagesize  = val;   //调用 selectAll 函数重新分页查询数据
                      
                    this.getBrandList();
                },
                handleCurrentChange(val) {
                    // 重新设置当前页码
                    this.currentPage  = val;
                    //调用 selectAll 函数重新分页查询数据
                    this.getBrandList();
                },

                //批量删除
                delPatch() {
                    for (var i = 0; i < this.multipleSelection.length; i++) {
                        let selectionElement = this.multipleSelection[i];
                        this.selectIds[i] = selectionElement.id;
                    }

                    axios.post("http://localhost/deleteByIds", this.selectIds).then((resp) => {
                        //刷新数据
                        if (resp.data == "sucess") {
                            this.getBrandList();

                            this.$message({
                                message: '恭喜你,删除成功',
                                type: 'success'
                            });
                        } else {
                            this.$message({
                                message: '删除失败',
                                type: 'error'
                            });
                        }
                    })
                },
                //修改
                updateBrand() {
                    //console.log(this.newBrand);
                    axios.post("http://localhost/updateData", this.newBrand).then((resp) => {
                        //刷新数据
                        if (resp.data == "sucess") {
                            this.getBrandList();
                            this.dialogVisible1 = false;
                            this.$message({
                                message: '恭喜你,修改成功',
                                type: 'success'
                            });
                        } else {
                            this.$message({
                                message: '修改失败',
                                type: 'error'
                            });
                        }
                    })
                },
                changeBrand(scope) {
                    //console.log(scope.row);
                    this.dialogVisible1 = true;
                    this.newBrand.brandName = scope.row.brandName;
                    this.newBrand.companyName = scope.row.companyName;
                    this.newBrand.description = scope.row.description;
                    this.newBrand.status = scope.row.status;
                    this.newBrand.ordered = scope.row.ordered;
                    this.newBrand.id = scope.row.id;
                },
                //单条数据的删除
                deleteById(id) {
                    //console.log(id);
                    axios.post("http://localhost/delete", id).then((resp) => {
                        //刷新数据
                        if (resp.data == "sucess") {
                            this.getBrandList();
                            this.$message({
                                message: '恭喜你,删除成功',
                                type: 'success'
                            });
                        } else {
                            this.$message({
                                message: '删除失败',
                                type: 'error'
                            });
                        }
                    })
                },
                //获取数据
                getBrandList() {
                    axios.get("http://localhost/selectByPage?currentPage=" + this.currentPage + "&pageSize=" + this.Pagesize).then((resp) => {
                        //刷新数据
                        this.tableData = resp.data.rows;
                        //总记录数
                        this.totalCount = resp.data.totalCount;
                    })
                }
            },
            data() {
                return {
                    selectId: 1,
                    // 当前页码
                    currentPage: 1,
                    //总共页码
                    totalCount: 400,
                    // 添加数据对话框是否展示的标记
                    dialogVisible: false,
                    dialogVisible1: false,
                    Pagesize: 5,
                    // 品牌模型数据
                    brand: {
                        status: '',
                        brandName: '',
                        companyName: '',
                        id: "",
                        ordered: "",
                        description: ""
                    },
                    rules: {

                    },
                    newBrand: {
                        status: '',
                        brandName: '',
                        companyName: '',
                        id: "",
                        ordered: "",
                        description: ""
                    },
                    rules: {

                    },
                    // 复选框选中数据集合
                    multipleSelection: [],
                    // 复选框选中数据集合
                    selectIds: [],
                    // 表格数据
                    tableData: [{

                    }]
                }
            },

            mounted() {
                this.getBrandList();
            }
        })
    </script>

</body>

</html>